package org.bee.controller;

import org.bee.PageUtil;
import org.bee.ResultTool;
import org.bee.enums.ResultCode;
import org.bee.exception.ExceptionUtils;
import org.bee.id.UUIDUtils;
import org.bee.input.PermissionInsertVo;
import org.bee.input.PermissionSearchVo;
import org.bee.input.PermissionUpdateVo;
import org.bee.model.Permission;
import org.bee.model.RolePermission;
import org.bee.model.result.Result;
import org.bee.output.PermissionOut;
import org.bee.secuirty.annotation.ApiAuth;
import org.bee.service.PermissionService;
import org.bee.service.RolePermissionService;
import org.bee.transaction.TransUtils;
import org.bee.transaction.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 接口权限表控制器
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    Logger logger = LoggerFactory.getLogger(PermissionController.class);

    private final PermissionService permissionService;

    private final RolePermissionService rolePermissionService;

    public PermissionController(PermissionService permissionService,
                                RolePermissionService rolePermissionService) {
        this.permissionService = permissionService;
        this.rolePermissionService = rolePermissionService;
    }

    /**
     * 新增接口权限表
     * @param permissionInsertVo 新增参数
     * @return 新增结果
     */
    @PostMapping("insert")
    @ApiAuth(value = "permission.insert",description = "接口权限新增")
    public Result insert(@RequestBody PermissionInsertVo permissionInsertVo) {
        Result result = ResultTool.success();
        Transaction trans = null;
        try {
            Permission permission = new Permission();
            BeanUtils.copyProperties(permissionInsertVo, permission);
            permission.setPermissionId(UUIDUtils.simpleUuid());
            trans= TransUtils.getTrans();
            trans.begin();
            permissionService.insert(permission);
            trans.commit();
        } catch (Exception e) {
            TransUtils.rollback(trans);
            result = ResultTool.fail();
            logger.error(e.getMessage(), ExceptionUtils.getFullStackTrace(e));
            return result;
        }
        return result;
    }

    /**
     * 删除接口权限表
     * @param id id
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @ApiAuth(value = "permission.delete",description = "接口权限删除")
    public Result delete(@RequestParam("id") String id) {
        Result result = ResultTool.success();
        Transaction trans = null;
        if (id == null|| id.isEmpty()) {
            result = ResultTool.fail(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        List<RolePermission> rolePermissions = rolePermissionService.selectByPermissionId(id);
        if (!rolePermissions.isEmpty()) {
            result = ResultTool.fail(ResultCode.DATA_IS_USED);
            result.setMsg("该接口权限有角色在使用，无法删除");
            return result;
        }
        try {
            trans= TransUtils.getTrans();
            trans.begin();
            permissionService.delete(id);
            trans.commit();
        } catch (Exception e) {
            TransUtils.rollback(trans);
            result = ResultTool.fail();
            logger.error(e.getMessage(), ExceptionUtils.getFullStackTrace(e));
            return result;
        }
        return result;
    }

    /**
     * 修改接口权限表
     * @param permissionUpdateVo 修改参数
     * @return 修改结果
     */
    @PostMapping("update")
    @ApiAuth(value = "permission.update",description = "接口权限修改")
    public Result update(@RequestBody PermissionUpdateVo permissionUpdateVo) {
        Result result = ResultTool.success();
        Transaction trans = null;
        try {
            Permission permission = new Permission();
            BeanUtils.copyProperties(permissionUpdateVo, permission);
            trans= TransUtils.getTrans();
            trans.begin();
            permissionService.update(permission);
            trans.commit();
        } catch (Exception e) {
            TransUtils.rollback(trans);
            result = ResultTool.fail();
            logger.error(e.getMessage(), ExceptionUtils.getFullStackTrace(e));
            return result;
        }
        return result;
    }

    /**
     * 分页查询查询接口权限表
     * @param permissionSearchVo 查询参数
     * @return 查询结果
     */
    @PostMapping("pageQuery")
    @ApiAuth(value = "permission.pageQuery",description = "接口权限分页查询")
    public Result select(@RequestBody PermissionSearchVo permissionSearchVo) {
        Result result = ResultTool.success();
        PageUtil.setPage(permissionSearchVo.getCurrent(),permissionSearchVo.getSize());
        List<Permission> select = permissionService.select(permissionSearchVo);
        List<PermissionOut> selectOut = new ArrayList<>();
        select.forEach(permission -> {
            PermissionOut permissionOut = new PermissionOut();
            BeanUtils.copyProperties(permission, permissionOut);
            selectOut.add(permissionOut);
        });
        result.setPageData(selectOut);
        return result;
    }

    /**
     * 根据id查询接口权限表
     * @param id id
     * @return 接口权限信息
     */
    @GetMapping("selectById")
    @ApiAuth(value = "permission.selectById",description = "接口权限")
    public Result selectById(String id) {
        Result result = ResultTool.success();
        Permission selectById = permissionService.selectById(id);
        PermissionOut permissionOut = new PermissionOut();
        BeanUtils.copyProperties(selectById, permissionOut);
        result.setData(permissionOut);
        return result;
    }
}
